package cn.com.gome.sn.inapi.utils;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import cn.com.gome.common.security.base64.Base64Util;
 
 
/**
 * 与无线端使用的aes加密
 * @author liuyang-ds26
 *
 */
public class APPAESUtils {
	public static String encrypt(String input, String key){
	byte[] crypted = null;
	try{
	SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
	Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
	cipher.init(Cipher.ENCRYPT_MODE, skey);
	crypted = cipher.doFinal(input.getBytes("UTF-8"));
	}catch(Exception e){
	System.out.println(e.toString());
	}
	return new String(Base64Util.encodeMessage(crypted));
}
 
public static String decrypt(String input, String key){
	byte[] output = null;
	try{
	SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
	Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
	cipher.init(Cipher.DECRYPT_MODE, skey);
	output = cipher.doFinal(Base64Util.decodeMessage(input));
	}catch(Exception e){
	System.out.println(e.toString());
	}
	try {
		return new String(output,"UTF-8");
	} catch (UnsupportedEncodingException e) {
		System.out.println(e.toString());
		return null;
	}
}
 
	public static void main(String[] args) {
		String key = "0000000000000000";
		String data = "{\"sign\":\"B5255719F74458018FF46550C18527CB\",\"systemNo\":\"0001\",\"encReqInfo\":\"viIIUbasgDHYGphDCW5KkYtkpKQci8DVRruGW8YIAIdNdRPL6Ly2dmsLRmFDjJjBRFnm19d5Ew0E6X2QRQFhRLNNgO4z487aPx0Yl637MVAoQD1FklTj5Ol4ZUBqFrPnWSP498+ojAVGXpHm6iW8WvgpW9KanU/idaooiqD7WMai7PyogTTSHGA/6GawXOPm+7Ot46dLecPbLhD6g9GVP8Msdj8lqFGfBEAuEuFbp5kwB3XSaQRvguwDdH6AUax4CD8VLhPE89lzAMxI0oKQoqZ+AXJfmdxrD3Nu9gszQRA=\",\"reqMessageId\":\"ZC20150413103200583883\",\"reqTime\":\"20150413103200\",\"resCode\":\"0000\",\"isSuccess\":\"Y\",\"resDesc\":\"支付单信息查询成功。\"}";
		
		System.out.println("加密后"+APPAESUtils.encrypt(data, key));
		
		System.out.println(APPAESUtils.decrypt(APPAESUtils.encrypt(data, key), key));
		
			
	}	
}